package org.wangpf.service.common;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.wangpf.util.SimpleHashMap;
/**
 * 转换成Map对象
 * @author wangpf
 *
 */
public class MapORTransform implements IORTransform {
	private Map<String, String> fieldMap=null;
	/**
	 * 一般用这种构造的时候 是由于 返回的某个field名字无法和sql列名匹配,就可以用这个map来做匹配,key 是sql现在的字段名,value是 需要返回的字段名
	 * @param m
	 */
	public MapORTransform(Map<String, String> m){
		this.fieldMap=m;
	}
	public MapORTransform(){}
	@Override
	public Object transform(long arg0, SimpleHashMap arg1) {
		Map<String, Object> returnMap=new HashMap<String, Object>();
		Set<?> keys = arg1.keySet();
		if(this.fieldMap!=null){
			String realkey="";
			for (Object key : keys) {
				realkey=key.toString();
				if(this.fieldMap.containsKey(realkey)){
					realkey=this.fieldMap.get(realkey).toString();
				}
				returnMap.put(realkey, arg1.get(key.toString()));
			}
		}else{
			for (Object key : keys) {
				returnMap.put(key.toString(), arg1.get(key.toString()));
			}
		}
		return returnMap;
	}

}
